import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/login', component: () => import('@/views/login/index.vue') }, // 登录页
    {
      path: '/',
      component: () => import('@/views/layout/index.vue'),
      redirect:'/book/manage',
      children: [
        {
          path: '/book/manage',
          component: () => import('@/views/book/BookManage.vue')
        },
        {
          path: '/book/channel',
          component: () => import('@/views/book/BookChannel.vue')
        },
        {
          path: '/book/chart',
          component: () => import('@/views/book/BookChart.vue')
        }
      ]
    }
  ],
})

router.beforeEach((to) => {
  // 如果没有token, 且访问的是非登录页，拦截到登录，其他情况正常放行
  const useStore = useUserStore()
  if (!useStore.token && to.path !== '/login') return '/login'
})

export default router
